package com.fp.m7.server.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fp.m7.api.manager.AccountManager;
import com.fp.m7.api.manager.FollowManager;
import com.fp.m7.api.model.Account;
import com.fp.m7.server.auth.AuthenticationChecker;
import com.fp.m7.server.helpers.UserDtoFactory;
import com.fp.m7.shared.dto.UserDto;
import com.fp.m7.shared.services.FollowService;

/**
 * The server side implementation of the Follows RPC service.
 */
@Service("follows")
@Transactional
public class FollowServiceImpl implements FollowService {

    @Autowired
    private AuthenticationChecker authChecker;

    @Autowired
    private AccountManager accountManager;

    @Autowired
    private FollowManager followManger;

    @Autowired
    private UserDtoFactory userDtoFactory;

    @Secured({ "ROLE_USER" })
    @Override
    public Boolean follow(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account follower = authChecker.getLoggedAccount();
            Account followed = accountManager.findById(userDto.getId());

            if (!follower.equals(followed) && followed != null) {
                followManger.follow(follower, followed);
                return true;
            }
        }
        return false;
    }

    @Secured({ "ROLE_USER" })
    @Override
    public Boolean unfollow(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account follower = authChecker.getLoggedAccount();
            Account followed = accountManager.findById(userDto.getId());

            if (!follower.equals(followed) && followed != null) {
                followManger.unfollow(follower, followed);
                return true;
            }
        }
        return false;
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public List<UserDto> listFollowed() {
        Account user = authChecker.getLoggedAccount();
        return listFollowed(userDtoFactory.build(user));
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public List<UserDto> listFollowers() {
        Account user = authChecker.getLoggedAccount();
        return listFollowers(userDtoFactory.build(user));
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public Integer countFollowed() {
        Account user = authChecker.getLoggedAccount();
        return countFollowed(userDtoFactory.build(user));
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public Integer countFollowers() {
        Account user = authChecker.getLoggedAccount();
        return countFollowers(userDtoFactory.build(user));
    }

    @Transactional(readOnly = true)
    @Override
    public List<UserDto> listFollowed(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account user = accountManager.findById(userDto.getId());

            if (user != null) {
                List<Account> users = followManger.getFollowedByUser(user);
                return userDtoFactory.build(users);
            }
        }
        return new ArrayList<UserDto>(0);
    }

    @Transactional(readOnly = true)
    @Override
    public List<UserDto> listFollowers(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account user = accountManager.findById(userDto.getId());

            if (user != null) {
                List<Account> users = followManger.getFollowersForUser(user);
                return userDtoFactory.build(users);
            }
        }
        return new ArrayList<UserDto>(0);
    }

    @Transactional(readOnly = true)
    @Override
    public Integer countFollowed(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account user = accountManager.findById(userDto.getId());
            return followManger.countActivelyFollowed(user);
        }
        return 0;
    }

    @Transactional(readOnly = true)
    @Override
    public Integer countFollowers(UserDto userDto) {
        if (userDto != null && userDto.getId() != null) {
            Account user = accountManager.findById(userDto.getId());
            return followManger.countActiveFollowers(user);
        }
        return 0;
    }

    public void setAuthChecker(AuthenticationChecker authChecker) {
        this.authChecker = authChecker;
    }
    
    public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
    
    public void setFollowManger(FollowManager followManger) {
		this.followManger = followManger;
	}
    
    public void setUserDtoFactory(UserDtoFactory userDtoFactory) {
        this.userDtoFactory = userDtoFactory;
    }
}
